package com.longrise.android.byjk.plugins.aboutme;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;

import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.FolderConstants;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.Tracker_Google;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by callxkj on 16/6/21.
 * 分享保宝的页面 邀请好友
 */
public class ShareAPPLFView extends BaseActivity implements View.OnClickListener {

    private LinearLayout mLLqqzone;
    private LinearLayout mLLwechat;
    private LinearLayout mLLpengyouquan;
    private LinearLayout mLLqq;
    private Toolbar mToolbar;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.lfview_shareapp;
    }

    @Override
    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle(getResources().getString(R.string.fxbaobao));
        setSupportActionBar(mToolbar);
        mLLqqzone = (LinearLayout) findViewById(R.id.shareapp_qzone_ll);
        mLLwechat = (LinearLayout) findViewById(R.id.shareapp_wechat_ll);
        mLLpengyouquan = (LinearLayout) findViewById(R.id.shareapp_pengyouquan_ll);
        mLLqq = (LinearLayout) findViewById(R.id.shareapp_qq_ll);
        initEvent();

        Tracker_Google.getInstance().addGoogleScreenName("邀请好友");
    }

    private void initEvent() {
        mLLqqzone.setOnClickListener(this);
        mLLwechat.setOnClickListener(this);
        mLLpengyouquan.setOnClickListener(this);
        mLLqq.setOnClickListener(this);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shareapp_qzone_ll:
                share(1);
                break;
            case R.id.shareapp_wechat_ll:
                share(2);
                break;
            case R.id.shareapp_pengyouquan_ll:
                share(3);
                break;
            case R.id.shareapp_qq_ll:
                share(4);
                break;
        }
    }

    /**
     * 分享
     *
     * @param flag 标记
     */
    private void share(int flag) {
        ShareSDK.initSDK(this);
        Platform platform = null;
        Platform.ShareParams sp = null;
        if (flag == 1) {
            platform = ShareSDK.getPlatform(QZone.NAME);
            sp = new QZone.ShareParams();
        } else if (flag == 2) {
            platform = ShareSDK.getPlatform(Wechat.NAME);
            sp = new Wechat.ShareParams();
        } else if (flag == 3) {
            platform = ShareSDK.getPlatform(WechatMoments.NAME);
            sp = new WechatMoments.ShareParams();
        } else {
            platform = ShareSDK.getPlatform(QQ.NAME);
            sp = new QQ.ShareParams();
        }
        sp.setTitle(getString(R.string.tjxz));
        sp.setTitleUrl(getString(R.string.tjurl));
        sp.setSiteUrl(getString(R.string.tjurl));
        sp.setUrl(getString(R.string.tjurl));
        sp.setSiteUrl(getString(R.string.tjurl));
        sp.setText(getString(R.string.tjnr));
        sp.setImagePath(FolderConstants.ICON_DIR + "/logoshare.jpg");
        sp.setShareType(Platform.SHARE_WEBPAGE);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                DZZWTools.showToast(ShareAPPLFView.this, "分享成功", 0);
//                ShareGetIntegralHelper.toShowIntegral(ShareAPPLFView.this,"inviteFriends");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                DZZWTools.showToast(ShareAPPLFView.this, "分享失败", 0);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                DZZWTools.showToast(ShareAPPLFView.this, "分享取消", 0);
            }
        }); // 设置分享事件回调
// 执行图文分享
        platform.share(sp);
        addGoogleEvent(flag);
    }

    private void addGoogleEvent(int flag) {
        switch (flag) {
            case 1:
                Tracker_Google.getInstance().addEvent("Share-app", "QQ-space", "QQ空间");
                break;
            case 2:
                Tracker_Google.getInstance().addEvent("Share-app", "WeChat-friends", "微信");
                break;

            case 3:
                Tracker_Google.getInstance().addEvent("Share-app", "Circle-friends", "朋友圈");
                break;

            default:
                Tracker_Google.getInstance().addEvent("Share-app", "QQ-friends", "QQ好友");
                break;
        }
    }
}
